package cn.sun.order.controller;

import cn.sun.basic.util.AjaxResult;
import cn.sun.basic.util.PageBean;
import cn.sun.order.domain.AdoptOrder;
import cn.sun.order.query.AdoptOrderQuery;
import cn.sun.order.service.IAdoptOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 员工接口
 *    crud
 *    分页+高级
 */
@RestController //@Controller+@ResponseBody
@RequestMapping("/adoptOrder")
@Api(value = "员工接口",description = "员工接口详细描述")
public class AdoptOrderController {
    @Autowired
    private IAdoptOrderService adoptOrderService;

    //添加或修改-差异在于是否有id put
    @PutMapping
    @ApiOperation(value = "员工添加或修改",notes = "如果有id就是修改否则就是添加！")
    public AjaxResult addOrUpdate(@RequestBody AdoptOrder adoptOrder){
        //通过id判断是添加或者修改
        try{
            if (adoptOrder.getId()!=null){
                adoptOrderService.update(adoptOrder);
            }else {
                adoptOrderService.save(adoptOrder);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存失败！"+e.getMessage());
        }

    }

    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id){

        try {
            adoptOrderService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除失败!"+e.getMessage());
        }
    }

    //获取一个 get
    @GetMapping("/{id}") //Get http://localhost:8080/deparment/1 deparment/2
    public AdoptOrder findById(@PathVariable("id") Long id){
        return adoptOrderService.findById(id);
    }

    //获取所有 get
    @GetMapping ///adoptOrder
    public List<AdoptOrder> findAll(){
        return adoptOrderService.findAll();
    }

    //分页+高级查询 //post /adoptOrder 传递对象都往@RequestBody
    @PostMapping
    public PageBean<AdoptOrder> findPage(@RequestBody AdoptOrderQuery query){
        return adoptOrderService.findPage(query);
    }

}
